
///<reference path="../typings/tsd.d.ts" />
///<reference path="dialog.ts" />
///<reference path="datebox.ts" />
///<reference path="selectword.ts" />
///<reference path="ajaxbutton_autotimer.ts" />
///<reference path="autotable.ts" />
///<reference path="autopanel.ts" />
(function($: JQueryStatic, global: Window) {
    $.auto = <AutoStatic>{};
    //日期格式化
    $.auto.frmDate = function(date, fmt) {
        let o = {
            "M+": date.getMonth() + 1,                 //月份
            "d+": date.getDate(),                    //日
            "h+": date.getHours(),                   //小时
            "m+": date.getMinutes(),                 //分
            "s+": date.getSeconds(),                 //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (let k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
    //弹窗
    $.auto.alert = function(title: string, msg: string, option?: ILayerOption) {
        let opt = $.extend({ title: title }, option);
        layer.alert(msg, opt);
    }
    $.auto.confirm = function(title: string, msg: string, yes?: () => void, cancel?: () => void) {
        layer.confirm(msg, {title:title},function(index) {
            yes&& yes();
            layer.close(index);
        },function(index) {
            cancel&&cancel();
            layer.close(index);
        });
    }
    //本地存储
    let wls = window.localStorage;
    $.auto.storage = {
        //不存在，返回null
        get: function(key) {
            if (wls) {
                let str = wls.getItem(key);
                try {
                    return JSON.parse(str);
                }
                catch (e) {
                    return str;
                }
            }
        },
        set: function(key, value) {
            if (wls) {
                return wls.setItem(key, _.isString(value) ? value : JSON.stringify(value));
            }
        }
    }
    //修正js中浮点数运算时产生精度误差，只保留最大精度,
    //t ,m 用于保留精度。 s是数字的字符串，p是.的位置  r是结果
    $.extend($.auto, {
        div: function() {
            if (arguments.length == 1) {
                return parseFloat(arguments[0]);
            }
            else {
                var t: number, m: number, s: string, p: number, r = arguments[0];
                s = arguments[0].toString();
                p = s.indexOf(".");
                m = (p > -1) ? (s.length - p - 1) : 0;
                for (var i = 1; i < arguments.length; i++) {
                    s = arguments[i].toString();
                    p = s.indexOf(".");
                    t = (p > -1) ? (s.length - p - 1) : 0;
                    m = t > m ? t : m;
                    r /= (parseFloat(arguments[i]));
                }
                return Number(r.toFixed(m));
            }
        },
        mul: function() {
            if (arguments.length == 1) {
                return parseFloat(arguments[0]);
            }
            else {
                var t = 0, m = 0, s, p, r = 1;
                for (var i = 0; i < arguments.length; i++) {
                    s = arguments[i].toString();
                    p = s.indexOf(".");
                    t = (p > -1) ? (s.length - p - 1) : 0;
                    m = t > m ? t : m;
                    r *= parseFloat(arguments[i]);
                }
                return Number(r.toFixed(m));
            }
        },
        add: function() {
            if (arguments.length == 1) {
                return parseFloat(arguments[0]);
            }
            else {
                var t = 0, m = 0, s, p, r = 0;
                for (var i = 0; i < arguments.length; i++) {
                    s = arguments[i].toString();
                    p = s.indexOf(".");
                    t = (p > -1) ? (s.length - p - 1) : 0;
                    m = t > m ? t : m;
                    r += parseFloat(arguments[i]);
                }
                return Number(r.toFixed(m));
            }
        },
        sub: function() {
            if (arguments.length == 1) {
                return parseFloat(arguments[0]);
            }
            else {
                var t, m, s, p, r = arguments[0];
                s = arguments[0].toString();
                p = s.indexOf(".");
                m = (p > -1) ? (s.length - p - 1) : 0;
                for (var i = 1; i < arguments.length; i++) {
                    s = arguments[i].toString();
                    p = s.indexOf(".");
                    t = (p > -1) ? (s.length - p - 1) : 0;
                    m = t > m ? t : m;
                    r -= parseFloat((arguments[i]));
                }
                return Number(r.toFixed(m));
            }
        }
    });
    //dialog
    $.fn["dialog"] = function(options: IDialogOption|string) {
        return this.each(function() {
            new AUTO.Dialog(this, options);
        });
    };
    $.fn["datebox"] = function(mode: string, options?: DatepickerOptions) {
        return this.each(function() {
            new AUTO.DateBox(this, mode, options);
        });
    };
    $.fn["selectWord"] = function() {
        return this.each(function() {
            new AUTO.SelectWord(this);
        });
    };
    $.fn["autoTable"] = function(options: AutoTableOptions | string) {
        if (options == "option") {
            return $(this).data("OriginData");
        } else {
            return this.each(function() {
                new AUTO.AutoTable(this, options);
            });
        }
    };
    $.fn["autoTimer"] = function(options: IAutoTimerOption | string) {
        return this.each(function() {
            new AUTO.AutoTimer(this, options);
        });
    };

    $.fn["ajaxButton"] = function(fun_def: (p) => JQueryDeferred<any>, fun_para: any) {
        return this.each(function() {
            new AUTO.AjaxButton(this, fun_def, fun_para);
        });
    };
    $.fn["tabpanel"] = function(cmd?: string, tabInfo?: ITabPanelInfo) {
        return this.each(function() {
            new AUTO.TabPanel(this, cmd, tabInfo);
        });
    };

})($, window)
